<template>
  <div class="app-container">
    <div class="filter-container">
      <template>
        <el-tabs>
          <el-tab-pane label="报班卡号" name="bbkh">
            <el-input v-model="listQuery.bbkh" placeholder="报班卡号" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
          </el-tab-pane>
          <el-tab-pane label="所属公司" name="yhxm">
            <el-input v-model="listQuery.ssgs" placeholder="所属公司" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
          </el-tab-pane>
          <el-tab-pane label="车牌照号" name="cpzh">
            <el-input v-model="listQuery.cpzh" placeholder="车牌照号" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
          </el-tab-pane>
          <el-tab-pane label="线路编号" name="xlbXlbh">
            <el-input v-model="listQuery.xlbXlbh" placeholder="线路编号" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
          </el-tab-pane>
        </el-tabs>
      </template>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column label="报班卡号" prop="bbkh" sortable="custom" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.bbkh }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车主姓名" prop="czxm" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.czxm }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车牌照号" prop="cpzh" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.cpzh }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属公司" prop="ssgs" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.gsmc }}</span>
        </template>
      </el-table-column>
      <el-table-column label="座位数" prop="zwgs" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.zwgs }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" class-name="status-col" width="80" align="center">
        <template slot-scope="{row}">
          <el-tag :type="row.clzt | statusFilter">
            {{ row.clzt }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="线路名称" prop="xlmc" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.xlmc }}</span>
        </template>
      </el-table-column>
      <el-table-column label="线路编号" prop="xlbXlbh" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.xlbXlbh }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">
        <el-form-item label="Type" prop="type">
          <el-select v-model="temp.type" class="filter-item" placeholder="Please select">
            <el-option v-for="item in calendarTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item label="Date" prop="timestamp">
          <el-date-picker v-model="temp.timestamp" type="datetime" placeholder="Please pick a date" />
        </el-form-item>
        <el-form-item label="Title" prop="title">
          <el-input v-model="temp.title" />
        </el-form-item>
        <el-form-item label="Status">
          <el-select v-model="temp.status" class="filter-item" placeholder="Please select">
            <el-option v-for="item in statusOptions" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        <el-form-item label="Imp">
          <el-rate v-model="temp.importance" :colors="['#99A9BF', '#F7BA2A', '#FF9900']" :max="3" style="margin-top:8px;" />
        </el-form-item>
        <el-form-item label="Remark">
          <el-input v-model="temp.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="Please input" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          Cancel
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          Confirm
        </el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogPvVisible" title="Reading statistics">
      <el-table :data="pvData" border fit highlight-current-row style="width: 100%">
        <el-table-column prop="key" label="Channel" />
        <el-table-column prop="pv" label="Pv" />
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogPvVisible = false">Confirm</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { show_clb_now1 } from '@/api/fuwu'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

const calendarTypeOptions = [
  { key: 'CN', display_name: 'China' },
  { key: 'US', display_name: 'USA' },
  { key: 'JP', display_name: 'Japan' },
  { key: 'EU', display_name: 'Eurozone' }
]

// arr to obj, such as { CN : "China", US : "USA" }
const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name
  return acc
}, {})

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    },
    typeFilter(type) {
      return calendarTypeKeyValue[type]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        importance: undefined,
        title: undefined,
        type: undefined,
        order_by:'bbkh_asc',
      },
      importanceOptions: [1, 2, 3],
      calendarTypeOptions,
      sortOptions: [{ label: 'ID Ascending', key: '+bbkh' }, { label: 'ID Descending', key: '-bbkh' }],
      statusOptions: ['published', 'draft', 'deleted'],
      showReviewer: false,
      temp: {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        type: '',
        status: 'published'
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      downloadLoading: false,
      czqx_list:['总控','报班','售票','服务台','检票','财务','查询'],
      newlist: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      show_clb_now1(this.listQuery).then(response => {
        console.log(response)
        this.list = response.data.items
        this.total = response.data.total
        // this.list = response.data
        // this.total = response.data
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作Success',
        type: 'success'
      })
      row.status = status
    },
    sortChange(data) {//排序发生变化事件
      const { prop, order } = data
      if (prop === 'bbkh') {
        this.sortByBBKH(order)
      }
    },
    sortByBBKH(order) {//按用户编号排序
      let cur = this.listQuery.order_by.split('_')
      if (order === 'ascending') {
        this.listQuery.order_by = cur[0]+'_'+'asc'
      } else {
        this.listQuery.order_by = cur[0]+'_'+'desc'
      }
      this.handleFilter()
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        status: 'published',
        type: ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleFetchPv(pv) {
      fetchPv(pv).then(response => {
        this.pvData = response.data.pvData
        this.dialogPvVisible = true
      })
    },
    formatJson(filterVal) {
        return this.list.map(v => filterVal.map(j => {
          if (j === 'timestamp') {
            return parseTime(v[j])
          } else {
            return v[j]
          }
        }))
      }
    }
}
</script>